﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading.Tasks;
//using System.Web;
//using System.Web.Mvc;
//using System.ComponentModel;
//using Super.Entity;
//using Super.Framework.Common;
//using Super.Framework.EF;
//using Super.Framework.Common.Web;
//using Super.Mvc;
//using Super.Service;
//using Super.Web.Areas.Admin.Models;

//namespace Developer.Web.Areas.Admin.Controllers
//{
//    [AuthDescription(Name = "角色管理", No = 1)]
//    public class RoleController : BaseController
//    {
//        #region 基础管理

//        [AuthDescription(Name = "角色列表", No = 1)]
//        public ActionResult Index()
//        {
//            PageSize = 20;
//            var pageIndex = Query.Value("page").ToInt(1);
//            var keyword = Query.Value("KeyWord");
//            var pageCount = 0;
//            var itemCount = 0;

//            var items = AuthService.GetRoleList(out itemCount, out pageCount, pageIndex: pageIndex, pageSize: PageSize, keyword: keyword);
//            ViewBag.pageCount = pageCount;
//            ViewBag.itemCount = itemCount;
//            ViewBag.keyWord = keyword;
//            return View(items);
//        }

//        [AuthDescription(Name = "角色删除", No = 1)]
//        public ActionResult Delete()
//        {
//            var id = Query.Value("id").ToInt(0);
//            var model = SysInfo.DB.Role.SingleOrDefault(a=>a.Id==id);
//            model.IsAbort = true;
//            SysInfo.DB.SaveChanges();
//            ShowMsg("操作成功!", MessageItemType.Right);
//            return RedirectToAction("Index", "Role");

//        }

//        [AuthDescription(Name = "角色产看", No = 1)]
//        public ActionResult Edit()
//        {
//            var id = Query.Value("Id").ToInt();
//            var model = new Role();
//            if (id > 0)
//            {
//                model = SysInfo.DB.Role.SingleOrDefault(a=>a.Id==id);
//            }
//            return View(model);
//        }

//        [AuthDescription(Name = "保存角色编辑", No = 1)]
//        [HttpPost]
//        public ActionResult Edit(int? z)
//        {
//            var id = Query.Value("Id").ToInt();
//            var name = Query.Value("name");
//            var description = Query.Value("description");
//            var code = Query.Value("code");
//            var isPlatform = Query.Value("IsPlatform") == "on";
//            var role = new Role();
//            if (id > 0)
//            {
//                role = SysInfo.DB.Role.FirstOrDefault(a=>a.Id==id);
//            }
//            else
//            {
//                role.CreateTime = DateTime.Now;
//                SysInfo.DB.Role.Add(role);
//            }

//            role.Name = name;
//            role.Description = description;
//            role.Code = code;
//            role.IsPlatform = isPlatform;
//            SysInfo.DB.SaveChanges();
//            ShowMsg("操作成功!", MessageItemType.Right);

//            return View(role);
//        }


//        #endregion
//        [AuthDescription(Name = "设置角色权限", No = 1)]
//        public ActionResult RoleActionSet()
//        {
//            PageSize = 20;
//            var id = Query.Value("id").ToInt(0);
//            var pageIndex = Query.Value("page").ToInt(1);
//            int itemCount = 0, pageCount = 0;
//            var keyword = string.Empty;
//            //获取角色对应的已经关联的权限项
//            var actionPermissionRole = BaseService<ActionPermissionRole>.GetAll().Where(a=>a.RoleId==id);
//            //获取用户选择的角色
//            ViewBag.role = BaseService<Role>.GetAll().FirstOrDefault(r=>r.Id==id);

//            //获取权限项列表
//            var powerList = AuthService.GetActionPermissionList(out itemCount, out pageCount, pageIndex: pageIndex,
//                pageSize: PageSize, keyword: keyword);

//            ViewBag.ControllerList = powerList.Where(a => a.ParentId == 0);

//            ViewBag.ActionList = powerList.Where(a => a.ParentId != 0);

//            return View(actionPermissionRole);
//        }

//        [HttpPost]
//        [AuthDescription(Name = "保存权限设置", No = 1)]
//        public ActionResult RoleActionSet(int? z)
//        {
//            var id = Query.Value("id").ToInt(0);
//            var actions = Query.Value("actionItem");
//            var aprItem = new ActionPermissionRole();
//            if (id > 0)
//            {
//                //old
//                var oldActionList = BaseService<ActionPermissionRole>.GetAll().Where(a=>a.RoleId==id);
//                if (actions == null)
//                {
//                    //如果传过来的权限项为空，就删除该角色下的所有权限
//                    AuthService.RemoveActionPermission(oldActionList);
//                }
//                else
//                {
//                    //now
//                    var actionList = actions.Split(',').ToList();// 1 2 3  
//                    //找不相同
//                    var diff = oldActionList.Where(a => !actionList.Contains(a.ActionPermissionId.ToString()));
//                    //删除
//                    AuthService.RemoveActionPermission(diff);
//                    //存新添加
//                    var oldActions = oldActionList.Select(c => c.ActionPermissionId);
//                    var nowlist = actionList.Select<string, int>(x => Convert.ToInt32(x)).Where(a => !a.IsIn<int>(oldActions));
//                    //添加
//                    aprItem.RoleId = id;
//                    foreach (var al in nowlist)
//                    {
//                        aprItem.ActionPermissionId = al;
//                        BaseService<ActionPermissionRole>.Add(aprItem);
//                    }
//                }
//            }
//            ShowMsg("操作成功", MessageItemType.Right);
//            return Redirect("/admin/role/RoleActionSet/?Id=" + id);
//        }

//    }
//}
